<?php 
session_start();
include '../connection/connection.php';
$tkemail=$_SESSION['tkemail'];
function getlang(){
		$lang = $_SESSION['lang'];
		if (empty($lang) || $lang == "EN") {
			$value = "../value/value_en.php";
		} else if($lang == "VN") {
			$value = "../value/value_vn.php";
		}
		else if($lang == "KR"){
			$value = "../value/value_kr.php";
		}
		return $value;
	}
	
	$value = getlang();
	include($value);
	$lang = $_SESSION['lang'];
	if($lang=='')
		$lang="EN";
		
if(isset($_REQUEST['action'])){
	
	if($_REQUEST['action']=="startLicense"){
		$id_license=$_REQUEST['id_license'];
		$time=$_REQUEST['time'];
		
		$info = getdate();
		$date = $info['mday'];
		$month1 = $info['mon'];
		$year = $info['year'];
		$date_start1 = "$year-$month1-$date";
		
		
		strtotime($info);
		$info=getdate(strtotime("+$time months"));
		$date = $info['mday'];
		$month1 = $info['mon'];
		$year = $info['year'];
		$date_stop1 = "$year-$month1-$date";
		
		if($time!=0){
			mysql_query("UPDATE license SET status='2', date_start='$date_start1', date_stop='$date_stop1' WHERE (id_license='$id_license')");
		}else{
			mysql_query("UPDATE license SET status='2', date_start='$date_start1' WHERE (id_license='$id_license')");
		}
	}
if($_REQUEST['action']=="stopLicense"){
		$id_license=$_REQUEST['id_license'];
		
		mysql_query("UPDATE license SET status='3' WHERE (id_license='$id_license')");
	}
}



    			$query=mysql_query("SELECT l.id_license,l.email,l.status,t.time,l.date_start,l.date_stop 
									FROM license l,type_license t 
									WHERE l.email='$tkemail' AND l.id_type_license=t.id_type_license;");
    			
				 $list_license=array();
				 $co=0;
				 while ($row=mysql_fetch_array($query)){
				 	if($row['status']==2){
				 		$co=2;
				 	}
				 	$list_license[$row['email']][]=array($row['time'],$row['status'],$row['date_start'],$row['date_stop'],$row['id_license']);
				 }
				 mysql_close($connection);
			?>
	       <table width="800" cellpadding="0" cellspacing="0" class="mtable">
	    	<thead>
	        	<tr>
	                <td align="center"><?=$address_email?></td>
	                <td align="center"><?=$type_license?></td>
	                <td align="center"><?=$namestatus?></td>
	                <td align="center"><?=$date_start?></td>
	                <td align="center"><?=$date_stop?></td>
	                <td align="center"><?=$start?></td>
	                
	            </tr>
	        </thead>
	       <?php 
	        foreach ($list_license as $email => $license){
	        	
	        	for($i=0;$i<count($license);$i++){	
	        	?>
	        	<?php
	        			
	        			if($license[$i][0]==0){
	        				$time=$free;
	        			}else{
	        				$time=$license[$i][0]." ".$month;
	        			}
	        			
	        			if($license[$i][1]==1){
	        				$color='white';
	        				$status=$status1;
	        			}
	        			if($license[$i][1]==""){
	        				$color='white';
	        				$status="";
	        				$time="";
	        			}
	        			if($license[$i][1]==2){
	        				$color='#B2DAF7';
	        				$status=$status2;
	        			}
	        			if($license[$i][1]==3){
	        				$color='#F1DFDC';
	        				$status=$status3;
	        			}
	        	if($i==0){
	        			
	        		
	        		?>
		        <tr>
		        		<td rowspan="<?=count($license)?>"><?=$email?></td>
		        	
		            		<td  style="background-color: <?=$color?>;" align="center" ><?=$time?></td>
			        		<td  style="background-color: <?=$color?>;" align="center"><?=$status?></td>
		            		<td  style="background-color: <?=$color?>;" align="center"><?=$license[$i][2]?></td>
		            		<td  style="background-color: <?=$color?>;" align="center"><?=$license[$i][3]?></td>
		            		<td style="background-color: <?=$color?>;" align="center">
					            <?php if($co==0)  if($license[$i][1]==1){?><a onclick="startLicense('<?=$license[$i][4]?>','<?=$license[$i][0]?>')" ><img src="images/edit.png" title="<?=$start?>" /></a><?php }?>
		            			<?php if($co==2)  if($license[$i][1]==2){?><a onclick="stopLicense('<?=$license[$i][4]?>')"><img src="images/xoa.png" title="<?=$del1?>" /></a><?php }?>
		            		</td>
		        </tr>
		        <?php }else{?>
	        	<tr>
		        		<td  style="background-color: <?=$color?>;" align="center"><?=$time?></td>
		        		<td  style="background-color: <?=$color?>;" align="center"><?=$status?></td>
	            		<td  style="background-color: <?=$color?>;" align="center"><?=$license[$i][2]?></td>
	            		<td  style="background-color: <?=$color?>;" align="center"><?=$license[$i][3]?></td>
	            		<td style="background-color: <?=$color?>;" align="center" >
	            			<?php if($co==0)  if($license[$i][1]==1){?><a  onclick="startLicense('<?=$license[$i][4]?>','<?=$license[$i][0]?>')"><img src="images/edit.png" title="<?=$start?>" /></a><?php }?>
		            		<?php if($co==2) if($license[$i][1]==2){?><a onclick="stopLicense('<?=$license[$i][4]?>')"><img src="images/xoa.png" title="<?=$del1?>" /></a><?php }?>
	            		</td>
	        	</tr>
	      	<?php } }
	        	}?>
	    </table>